package cn.wolfcode.trip.app.filter;


import cn.wolfcode.trip.base.util.UploadUtil;
import org.apache.commons.io.FileUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

public class ImageFilter implements Filter{

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        String uri = req.getRequestURI();
        //System.out.println(uri);
        File file = new File(UploadUtil.PATH,uri);
        if (file.exists()) {
            response.getOutputStream().write(FileUtils.readFileToByteArray(file));
        }else {
            chain.doFilter(request,response);
        }

    }

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void destroy() {

    }
}
